﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace Cn.Finder.Win32API
{
    public class WinApiUtils
    {

        public static class SendMessage
        {

            /// <summary>
            /// 发送文本数据到指定窗体
            /// </summary>
            /// <param name="hWin"></param>
            /// <param name="msgTxt"></param>
            /// <returns></returns>
            public static int WM_SETTEXT(IntPtr hWin, string msgTxt)
            {
                IntPtr strPtr = Marshal.StringToCoTaskMemUni(msgTxt);

                int ret = Win32API.Interface.WinUser.SendMessage(hWin, Win32API.Interface.MessageType.WM_SETTEXT, IntPtr.Zero.ToInt32(), strPtr.ToInt32());
                return ret;

            }


            /// <summary>
            /// 获取指定窗体的文本
            /// </summary>
            /// <param name="hWin"></param>
            /// <param name="bufferSize"></param>
            /// <returns></returns>
            public static string WM_GETTEXT(IntPtr hWin, int bufferSize)
            {
                string retstr=null;
                IntPtr bufferPtr=Marshal.AllocHGlobal(bufferSize);
                int ret = Win32API.Interface.WinUser.SendMessage(hWin, Win32API.Interface.MessageType.WM_GETTEXT, bufferSize, bufferPtr.ToInt32());
                if (ret>0)
                {
                    //返回成功
                    retstr=Marshal.PtrToStringAnsi(bufferPtr);
                    return retstr;
                }else{
                    throw new Exception(" WM_GETTEXT => error");
                }

            }

            /// <summary>
            /// 设置焦点到某个窗体
            /// </summary>
            /// <param name="hWin"></param>
            /// <returns></returns>
            public static bool WM_SETFOCUS(IntPtr hWin)
            {
                int ret=Win32API.Interface.WinUser.SendMessage(hWin, Win32API.Interface.MessageType.WM_SETFOCUS, IntPtr.Zero.ToInt32(), IntPtr.Zero.ToInt32());
                if (ret == 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }


            
        }

        /// <summary>
        /// 设置焦点到某个窗体
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        public static bool SetFocus(IntPtr hWnd)
        {
            IntPtr ptr=Win32API.Interface.WinUser.SetFocus(hWnd);
            return true;
        }

        /// <summary>
        /// 获取指向窗口的句柄
        /// </summary>
        /// <returns></returns>
        public static IntPtr GetFocus()
        {
            IntPtr hWnd=Win32API.Interface.WinUser.GetFocus();
            return hWnd;
        }



        public static IntPtr FindWindow(string className, string title)
        {
            IntPtr hWin = Win32API.Interface.WinUser.FindWindow(className, title);//正确获取
            return hWin;
        }

        public static IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, string lpszClass, string lpszWindow)
        {
            IntPtr hWin = Win32API.Interface.WinUser.FindWindowEx(hwndParent, hwndChildAfter, lpszClass, lpszWindow);
            return hWin;
        }

        /// <summary>
        /// 导入为windows窗体设置焦点的方法
        /// </summary>
        /// <param name="hWnd"></param>
        /// <returns></returns>
        public static bool SetForegroundWindow(IntPtr hWnd)
        {

            if (hWnd != IntPtr.Zero)
            {
                Win32API.Interface.WinUser.SetForegroundWindow(hWnd);
                return false;
            }
            return true;
        }



        /// <summary>
        /// 发送粘贴快捷键  CRTL+V
        /// </summary>
        /// <returns></returns>
        public static bool Paste()
        {
            Win32API.Interface.WinUser.keybd_event(0x11, 0, 0, 0); //按下CTRL
            Win32API.Interface.WinUser.keybd_event(86, 0, 0, 0); // 按下 V 键

            Win32API.Interface.WinUser.keybd_event(0x11, 0, 2, 0);//松开CTRL
            Win32API.Interface.WinUser.keybd_event(86, 0, 2, 0);// 松开 V 键

            return true;
        }

        /// <summary>
        /// 发送回车按键
        /// </summary>
        /// <returns></returns>
        public static void PressEnter()
        {
            Win32API.Interface.WinUser.keybd_event(0xD, 0, 0, 0); //按下回车

            Win32API.Interface.WinUser.keybd_event(0xD, 0, 2, 0);// 松开回车
        }


        /// <summary>
        /// 写入到ini文件
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="val"></param>
        /// <param name="filePath"></param>
        public static void WritePrivateProfileString(string
             section,
             string key, string val, string filePath)
        {
            Win32API.Interface.Kernel.WritePrivateProfileString(section, key, val, filePath);
        }


        /// <summary>
        /// 从ini文件读取
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="def"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static  string GetPrivateProfileString(string section,
             string key, 
              string filePath)
        {
            StringBuilder temp = new StringBuilder(1000);
            int i = Win32API.Interface.Kernel.GetPrivateProfileString(section, key, "", temp, 1000, filePath);
            return temp.ToString(); 
        }
    }
}
